<?php
/**
 * @package    Joomla.Tutorials
 * @subpackage Components
 * @link http://docs.joomla.org/Developing_a_Model-View-Controller_Component_-_Part_1
 * @license    GNU/GPL
*/
 
// no direct access
 
defined( '_JEXEC' ) or die( 'Restricted access' );
 
jimport( 'joomla.application.component.view');
jimport( 'joomla.filesystem.path' );

jimport( 'joomla.database.database' );
include JPATH_COMPONENT.DS.'functions.php';
/**
 * HTML View class for the HelloWorld Component
 *
 * @package    HelloWorld
 */
 
class wotclanViewsearch extends JView
{
    function display($tpl = null)
    {
    	if (JFactory::getUser()->authorise('core.add', 'com_wotclan')) 
      {
    	  $greeting = "Les tanks de la DB!";
        $this->assignRef( 'greeting', $greeting );
        parent::display($tpl);
   	    $db =& JFactory::getDbo();
      
        $query = $db->getQuery(true);
        
        if($_POST["user"] != ""){ 
      	  $MYWHERE="WHERE `name`='".$_POST['user']."'";     	
        }
        if($_GET["user"] != "" ){ 
      	  $MYWHERE="WHERE `name`='".$_GET['user']."'";     	
        }
        
      	if ($_POST["clan"] != ""){
      		if ($_POST['clan'] == "SANSCLAN") {
      			$search_clan = "";
      		} else {
      			$search_clan = $_POST["clan"];
      		}
      	}
      	if ($_GET["clan"] != ""){
      		if ($_GET['clan'] == "SANSCLAN") {
      			$search_clan = "";
      		} else {
      			$search_clan = $_GET["clan"];
      		}
      	}
      	if ($search_clan != ""){	
      		if ($MYWHERE == ""){
      			$MYWHERE=" WHERE `clan`='".$search_clan."'";
      		}	else {
      			$MYWHERE=" AND `clan`='".$search_clan."'";
      		}
      		
      	}
      	
      	
      	
      	
      	
     //   if($_POST["user"] != ""){ 
      	//  $MYWHERE="WHERE `name`='".$_POST['user']."'";     	
       // }
      //if ($_POST["clan"] != ""){
      	//if ($_POST['clan'] == "SANSCLAN") {$_POST['clan'] = "";}
      //	echo "Request clan est pas null";
     // 	if ($MYWHERE != ""){
      		
      	//	echo "<br>Request clan est pas null et user est pas null";
      //		$MYWHERE=$MYWHERE." AND `clan`='".$_REQUEST['clan']."'";
      		
      	//} else {
      	//	echo "<br>Request clan est pas null et user est null";
      		//$MYWHERE=" WHERE `clan`='".$_POST['clan']."'";
      //	}
   //   }
      //echo "<br>MYWHERE : ".$MYWHERE;
      $query="SELECT * FROM `#__wotclan_account`";
      $ltop=toptanks();
      $eltop=explode("|",$ltop);
      //echo "<h1>ltop:$ltop</h1>";
      foreach ($eltop as $seltop){
      	if ($WTOP == ""){
      		$WTOP="tanks LIKE '%".$seltop."%'";
      	} else {
      		$WTOP=$WTOP." OR tanks LIKE '%".$seltop."%'";
      	}
      
      }
      if  ($MYWHERE != ""){
      	if ($_POST["TOP"] || $_GET["TOP"]){	
       		$query=$query. " " .$MYWHERE . " AND (".$WTOP.")";
      	} else {
      		$query=$query. " " .$MYWHERE;
      	}
      } else {
      	if ($_POST["TOP"] || $_GET["TOP"]){	
      		$query=$query. " WHERE ".$WTOP;
      	} 
    	}
      
      
      if ($_GET['from']==""){
      	$from=0;
      } else {
      	$from=$_GET['from'];
      }
      if ($_GET['ninc']==""){
      	$ninc=200;
      } else {
      	$ninc=$_GET['ninc'];
      }
      $db->setQuery($query);
      $db->query();
      $max = $db->getNumRows();
      $query=$query. " LIMIT ".$from.",".$ninc ;
      //echo "<h1>$query</h1>";
      $db->setQuery($query);
      $db->query();
      $num_rows = $db->getNumRows();
      $rows = $db->loadObjectList();
      //echo "<h1>$query</h1>";
      echo "<div align='center'>";
      foreach ( $rows as $row ) {
      	
    		echo "<table><tr><td width='250' align='center' style='border:1px solid white'><a href='?view=listtanks&user=".$row->name."'>".$row->name."</a>";
    		if ($row->clan != ""){
    			echo " [<a href='?view=listtanks&clan=".$row->clan."'>".$row->clan."</a>]";
    		}
    		echo "</td><td width='250' align='center' style='border:1px solid white'>";
    		
    		$tank = explode("|",$row->tanks);
        //echo $row->tanks."<br>";
    		$x=0;
    		
      	while ($tank[$x] != ""){
    		  $query2 = $db->getQuery(true);
    		  $query2="SELECT * FROM `#__wotclan_tanks` WHERE `uniqid`='".$tank[$x]."'";
        //  echo "<br>".$query2."<br>";
    		  $db->setQuery($query2);
      	  $db->query();
      	  $num_rows2 = $db->getNumRows();
      	  $rows2 = $db->loadObjectList();
      	  
        	if ($num_rows2 == 1){
        		showimgtank($rows2[0]->name,$rows2[0]->common_name,$rows2[0]->tanktype,$rows2[0]->uniqid);
        		
    			} elseif ($num_rows2 == 0) {
          	echo "<tr>Prob avec le tank ".$x."(".$tank[$x].") : aucun tank</tr>";
          	if (preg_match("/".$tank[$x]."|/",$row->tanks)){
      				$newstring=preg_replace("/".$tank[$x]."|/","",$row->tanks);
      				
      			} else if (preg_match("/|".$tank[$x]."/",$row->tanks)){
							$newstring=preg_replace("/|".$tank[$x]."/","",$row->tanks);
			  		} else {
			  			$newstring=preg_replace("/".$tank[$x]."/","",$row->tanks);
			  		}
			  		$newstring=preg_replace("/\|$/","",$newstring);
			  		$query3 = $db->getQuery(true);
			  		$query3="UPDATE `#__wotclan_account` SET `tanks`='".$newstring."' WHERE `uniqid`='".$row->uniqid."';";
			  		
   	    		$db->setQuery($query3);
      			$db->query();
      			echo "<h1>newstring= ".$newstring." query : $query3</h1>";
          	//deletetank($tank[$x],$row->uniqid)
          } else {
           	echo "<tr>Prob avec le tank ".$x."(".$tank[$x].") : trop de tank</tr>";
          }
          if ($_GET["debug"]){
          	echo " (".$tank[$x].") ";
          }
          //echo "<a href='?view=delete&type=utank&tankid=".$tank[$x]."&account=".$row->uniqid."'>  Effacer</a>";
          echo "<br>";
        
        
		    $x++;
    		
  		  }
  		echo "</table>";
  	 
			}
			if ($from==0){
				$lmax=$ninc-1;
				echo "0-".$lmax ;
			} else {
				$lmax=$ninc-1;
				echo "<a href='?view=search&from=0&ninc=".$ninc."'>0-".$lmax."</a>";
				
			}
			if (($from-5*$ninc) < $lmax){
				$z=$ninc;
			} else {
				echo " ...";
				$z=$from-5*$ninc;
			}
			
			$z1=1;
			
		  while ($z < $max){
		  	if ($z1>10){
		  		echo " ...";
		  		$z=$max-$ninc;
		  		$z1=1;
		  	} else {
		  		if ($from == $z){
		  			echo " ".$z."-".$lmax;	
		  			$z=$z+$ninc;
		  			$z1=$z1+1;
		  		} else {
		  			$lmax=$z+$ninc-1;
		  			echo " <a href='?view=search&from=".$z."&ninc=".$ninc."'>".$z."-".$lmax."</a>";	
		  			$z=$z+$ninc;
		  			$z1=$z1+1;
		  		}
		  	}
		  }
		  echo "</div>";

      }
    
    }
}


